package net.hvacapp.cmvrfforandroid.ui.mine;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.view.View;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_suggestion_manager)
public class SuggestionManagerActivity extends BaseActivity implements TabLayout.OnTabSelectedListener{

    private final int Add_New_Suggestion = 1;

    @ViewById(R.id.user_approve_tablayout)
    TabLayout mProjectTabL;

    SuggestionListFragment mFragmentToMe;
    SuggestionListFragment mFragmentForMe;

    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();

        initEvent();
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "意见管理", null);
//        int ids[] = {R.id.action_new_suggestion};
//        int icons[] = {R.drawable.new_project};
//        setupToolbarMenuItem(ids, icons, new String[]{"提意见"}, null);
        setButton1Info("发表意见", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(SuggestionManagerActivity.this, AnnotationUtil.get(PublishActivity.class));
                        intent.putExtra("type", 1);
                        startActivityForResult(intent, Add_New_Suggestion);
                    }
                }
        );
    }

    /**
     * toolbar上的按钮点击事件
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_new_suggestion:
                Intent intent = new Intent(this, AnnotationUtil.get(PublishActivity.class));
                intent.putExtra("type", 1);
                startActivityForResult(intent, Add_New_Suggestion);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        mProjectTabL.addTab(mProjectTabL.newTab().setText("下属意见"), true);
        mProjectTabL.addTab(mProjectTabL.newTab().setText("自提意见"), false);
        mProjectTabL.setOnTabSelectedListener(this);

        setTabSelection(0);

    }



    private void initEvent() {

    }

    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }


    /**
     * 设置tab页
     * @param position 0:概况  1:关键词  2:排名
     */
    private void setTabSelection(int position) {
        // 开启一个Fragment事务
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);

        switch (position) {
            case 0:
                if (mFragmentToMe == null) {
                    mFragmentToMe = SuggestionListFragment_.builder().arg("typeValue", 0).build();
                    transaction.add(R.id.content_fl, mFragmentToMe);
                } else {
                    transaction.show(mFragmentToMe);
                }
                break;
            case 1:
                if (mFragmentForMe == null) {
                    mFragmentForMe = SuggestionListFragment_.builder().arg("typeValue", 1).build();
                    transaction.add(R.id.content_fl, mFragmentForMe);
                } else {
                    transaction.show(mFragmentForMe);
                }
                break;
        }

        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mFragmentToMe != null) {
            transaction.hide(mFragmentToMe);
        }
        if (mFragmentForMe != null) {
            transaction.hide(mFragmentForMe);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                setTabSelection(0);
                break;
            case 1:
                setTabSelection(1);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    /**
     * 回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != this.RESULT_OK) {
            return;
        }
        if (requestCode == Add_New_Suggestion) {
            if (mFragmentForMe != null) {
                mFragmentForMe.refreshData();
            }
        }
    }
}
